package org.andiv.android.esemeser;

import java.lang.reflect.Field;
import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
/**
 * Activity for updating msgdata record. Uses xml screen layout definition in update.xml, gets some text from fields and - spinner. 
 * Yes spinner, which was extremly easy to implement with Commander Data in my (subconscious) mind  
 * @author andrej
 *
 */
public class MsgEditor extends Activity {
  int _id;
  EditText mDesc, mPhone, mBody;
  Button mBSave;
  Spinner mSpinIcns;
  ArrayList<Integer> fldmap = null;
  
  @Override
  protected void onCreate(android.os.Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.update);
    mDesc = (EditText) findViewById(R.id.EditDesc);
    mPhone = (EditText) findViewById(R.id.EditPhone);
    mBody = (EditText) findViewById(R.id.EditBody);
    mBSave = (Button) findViewById(R.id.ButtonSave);
    mSpinIcns = (Spinner) findViewById(R.id.SpinnerIcns);
    manageSpinnerIcns();
    String action = getIntent().getStringExtra(MsgChooser.SMS_ACTION);
    _id = getIntent().getIntExtra(MsgChooser.MSG_ID, -1);
    Cursor c = MsgChooser.getDb().getWritableDatabase().query("msgdata", null, "_id = "+_id, null, null, null, null);
    c.moveToFirst();
    if (action.equals("add")) {
      //MsgChooser.getDb().insertFrom(_id);
    }
    if (!c.isNull(1)) mDesc.setText(c.getString(1));
    if (!c.isNull(2)) mPhone.setText(c.getString(2));
    if (!c.isNull(3)) mBody.setText(c.getString(3));
    if (!c.isNull(5)) mSpinIcns.setSelection(getSpinerPos(c.getInt(5)), false);
    mBSave.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        onBSave();
      }

    });
  };
  
  
  private int getSpinerPos(int integer) {
    return getIconResources().indexOf(integer);
  }

  /**
   * Thought this will be more complicated to do and extracted it in separate method.
   */
  private void manageSpinnerIcns() {
    ImageSpinnerAdapter isa = new ImageSpinnerAdapter(this, getIconResources());
    mSpinIcns.setAdapter(isa);
  }
  
  /**
   * Just showing off with little reflection, but with custom iconset in mind.
   * @return
   */
  private ArrayList<Integer> getIconResources() {
    if (fldmap != null) return fldmap;
    fldmap = new ArrayList<Integer>();
    Field[] dflds = R.drawable.class.getDeclaredFields();
    for (int i = 0; i < dflds.length; i++) {
      try {
        fldmap.add(dflds[i].getInt(null));
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return fldmap;
  }

  private void onBSave() {
//    MsgChooser.getDb().saveMsgdata(_id, new String[] {mDesc.getText().toString(), mPhone.getText().toString(), mBody.getText().toString() });
    ContentValues cv = new ContentValues();
    cv.put(Db.msgdata_fields[0], mDesc.getText().toString());
    cv.put(Db.msgdata_fields[1], mPhone.getText().toString());
    cv.put(Db.msgdata_fields[2], mBody.getText().toString());
    cv.put(Db.msgdata_fields[4], getIconResources().get(mSpinIcns.getSelectedItemPosition()));
    MsgChooser.getDb().updateMsgData(_id, cv);
    MsgChooser.getCursor().requery();
    this.finish();
  }
  
  /**
   * Adapter for spinner backed up with ArrayList filled with icon definitions via {@link MsgEditor#getIconResources()} 
   * @author andrej
   *
   */
  public class ImageSpinnerAdapter extends BaseAdapter {

    ArrayList<Integer> data;
    Context context;
    
    public ImageSpinnerAdapter(Context _c, ArrayList<Integer> _data) {
      data = _data;
      context = _c;
    }

    public int getCount() {
      return data.size();
    }

    public Object getItem(int position) {
      return position;
    }

    public long getItemId(int position) {
      return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
      ImageView iv = new ImageView(context);
      iv.setImageResource(data.get(position));
      iv.setAdjustViewBounds(true);
      
      return iv;
    }

    
  }
}
